package com.longxi.tianjintianbao.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.model.approval.NextNode;

import java.util.List;

/**
 * Created by cxw on & 2018/11/5
 */
public class AssetsWebLoginDialog extends Dialog {
    public AssetsWebLoginDialog(@NonNull Context context) {
        super(context);
    }

    public AssetsWebLoginDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected AssetsWebLoginDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context context;
        private OnLoginListener listener;
        String title = "未定义标题";

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setTitle(String title){
            this.title = title;
            return this;
        }

        public Builder setListener(OnLoginListener listener) {
            this.listener = listener;
            return this;
        }

        private TextView tv_title;
        private EditText etUsername;
        private EditText etPassword;
        private TextView tv_cancle;
        private TextView tv_login;
        private AssetsWebLoginDialog dialog;

        public AssetsWebLoginDialog create() {
            dialog = new AssetsWebLoginDialog(context);
            View view = LayoutInflater.from(context).inflate(R.layout.dialog_assets_web_login, null);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//加上这一句，取消原来的标题栏，没加这句之前，发现在三星的手机上会有一条蓝色的线
            dialog.setContentView(view, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            tv_title = view.findViewById(R.id.tv_title);
            etUsername = view.findViewById(R.id.etUsername);
            etPassword = view.findViewById(R.id.etPassword);
            tv_login = view.findViewById(R.id.tv_login);
            tv_cancle = view.findViewById(R.id.tv_cancle);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            tv_title.setText(title);
            tv_cancle.setOnClickListener(v -> dialog.dismiss());
            tv_login.setOnClickListener(v -> listener.onLogin(etUsername.getText().toString().trim(), etPassword.getText().toString().trim()));
            return dialog;
        }

        public void setUsernameAndPassword(String username, String password){
            etUsername.setText(TextUtils.isEmpty(username) ? "" : username);
            etPassword.setText(TextUtils.isEmpty(password) ? "" : password);
        }

        public void show(){
            if(dialog != null){
                dialog.show();
            }
        }

        public void dismiss(){
            dialog.dismiss();
        }
    }

    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;

        getWindow().getDecorView().setPadding(25, 0, 25, 25);

        getWindow().setAttributes(layoutParams);
    }

    public interface OnLoginListener {
        void onLogin(String username, String password);
    }

}
